/*
 *   Copyright (C) 2010  Jens Kieselbach
 * 
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.sql

import java.sql.*

class SqlResult {

    ResultSet result
    SqlField[] fields

    SqlResult(ResultSet res, List fds) {

        result = res
        fields = fds as SqlField[]
    }

    SqlResult(ResultSet result, SqlField[] fds) {

        result = res
        fields = fds
    }

    void each(Closure closure) throws SQLException {

        def values = new ArrayList(fields.length)
        while (result.next()) {
            values.clear()
            for(SqlField field : fields) {
                values << field.readFromResultSet(result)
            }

            if(fields.length == 1) {
                closure.call(values[0])
            }
            else {
                closure.call(values)
            }
        }
        result.close()
    }
}

